<?php
namespace app\api\controller;

use app\api\model\Menu as MenuModel;
use think\Db;

class Menu
{

// 组装树
    public function Createtree()
    {
        $objArr = array();

        // $arr = array(
        //     array("deptid" => 1, "parentid" => 0, 'deptname' => '栏目一'),
        //     array("deptid" => 2, "parentid" => 0, 'deptname' => '栏目二'),
        //     array("deptid" => 3, "parentid" => 1, 'deptname' => '栏目三'),
        //     array("deptid" => 4, "parentid" => 2, 'deptname' => '栏目四'),
        //     array("deptid" => 5, "parentid" => 1, 'deptname' => '栏目五'),
        //     array("deptid" => 6, "parentid" => 5, 'deptname' => '栏目六'),

        // );

        $arr = Db::query('select * from sys_menu');

        $refer = array();
        $tree = array();
        foreach ($arr as $k => $v) {
            $refer[$v['menuid']] = &$arr[$k]; //创建主键的数组引用
        }
        foreach ($arr as $k => $v) {
            $pid = $v['parentid']; //获取当前分类的父级id
            if ($pid == 0) {
                $tree[] = &$arr[$k]; //顶级栏目
            } else {
                if (isset($refer[$pid])) {
                    $refer[$pid]['children'][] = &$arr[$k]; //如果存在父级栏目，则添加进父级栏目的子栏目数组中
                }
            }
        }
        return json(['result' => 'ok', 'data' => $tree]);
    }

    /*
     *  菜单管理
     *  全站使用post
     *  2018-12-13
     *  add(添加) edit(修改）index(列表) delete(删除)
     *  'code'  http状态  msg 返回数据提示  result 成功ok 失败 error,
     *
     */

    //   添加菜单
    public function add()
    {
        if (!($_POST)) {

            return json([
                'code' => 400,
                'msg' => '请求错误,请使用POST',
            ]);
        } else {
            $menu['menuid'] = strtotime(date('Y-m-d H:i:s', time())); //添加时间
            $menu['menuname'] = $_POST['menuname']; //用户名
            $menu['menuicon'] = $_POST['menuicon']; //密码
            $menu['menusort'] = $_POST['menusort']; //手机号
            $menu['menuurl'] = $_POST['menuurl'];
            $menu['parentid'] = $_POST['parentid']; //部门id
            $menu['menustate'] = $_POST['menustate']; //权限id
            $menu['addtime'] = strtotime(date('Y-m-d H:i:s', time())); //添加时间
            $menu['addperson'] = 'admin'; // $_POST['addperson']; //添加人
            if ($result = MenuModel::create($menu)) {
                return json([
                    'result' => 'ok',
                    'msg' => '添加成功',
                ]);

            } else {

                return json([
                    'result' => 'error',
                    'msg' => '添加失败',
                ]);
            }
        }
    }

    //   修改用户
    public function edit()
    {
        if (!($_POST)) {

            return json([
                'code' => 400,
                'msg' => '请求错误,请使用POST',
            ]);

        } else {

            $id = $_POST['menuid']; //用户ID
            if ($id == "") {

                return json([
                    'code' => 400,
                    'msg' => 'id没传呀',
                ]);
            }
            $menu = MenuModel::get($id);
            if ($menu == "") {

                return "id 错误";
            }
            // $menu['menuid'] = $menu['menuid']; //用户ID
            $menu['menuname'] = $_POST['menuname'] != "" ? $_POST['menuname'] : $menu->menuname; //用户名
            $menu['menuicon'] = $_POST['menuicon'] != "" ? $_POST['menuicon'] : $menu->menuicon; //密码
            $menu['menusort'] = $_POST['menusort'] != "" ? $_POST['menusort'] : $menu->menusort; //手机号
            $menu['menuurl'] = $_POST['menuurl'] != "" ? $_POST['menuurl'] : $menu->menuurl; //手机号

            $menu['parentid'] = $_POST['parentid'] != "" ? $_POST['parentid'] : $menu->parentid; //id
            $menu['menustate'] = $_POST['menustate'] != "" ? $_POST['menustate'] : $menu->menustate; //权限id
            $menu['addtime'] = date('Y-m-d', time()); //添加时间
            $menu['addperson'] = $_POST['addperson'] != "" ? $_POST['addperson'] : $menu->addperson; //添加人
            $res = $menu->save();

            if ($res) {

                return json([
                    'result' => 'ok',
                    'msg' => '更新成功',
                ]);

            } else {

                return json([
                    'result' => 'error',
                    'msg' => '更新失败',
                ]);
            }
        }
    }

    // 获取列表
    public function index()
    {

        $list = MenuModel::all();
        return json(['result' => 'ok', 'data' => $list]);
    }

    // 删除数据
    public function delete()
    {
        $id = $_POST['menuid']; //用户ID

        if (!($_POST)) {
            return json([
                'code' => 400,
                'msg' => '请求错误,请使用POST',
            ]);
        } else {

            if ($id == "") {
                return json([
                    'code' => 400,
                    'msg' => 'id没传呀',
                ]);
            }

            $menu = MenuModel::get($id);

            if ($menu) {
                $menu->delete();
                return json([
                    'result' => 'ok',
                    'msg' => '删除成功',
                ]);

            } else {
                return json([
                    'result' => 'error',
                    'msg' => '删除失败',
                ]);
            }
        }
    }

}
